<template>
  <div class="login-container">
    <div class="login-box">
      <!--头像-->
      <div class="avatar-box">
        <img src="../../assets/logo.png" />
      </div>
      <!--登录-->
      <el-form
        @submit.native.prevent
        ref="loginFromRef"
        :rules="loginFromRules"
        :model="loginFrom"
        label-width="0px"
        class="login-from"
      >
        <!--用户名-->
        <el-form-item prop="username">
          <el-input v-model="loginFrom.username" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <!--密码-->
        <el-form-item prop="password">
          <el-input type="password" v-model="loginFrom.password" prefix-icon="iconfont icon-danju"></el-input>
        </el-form-item>
        <!--按钮-->
        <el-form-item>
          <el-button native-type="submit" type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resLoginFrom">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { setSessionStoreItem } from "../../utils/utils.js";

export default {
  name: "Login",
  data() {
    return {
      //登录表单的数据
      loginFrom: {
        username: "1",
        password: "1"
      },
      //表单验证规则
      loginFromRules: {
        //验证用户名
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
          { min: 1, max: 6, message: "长度在 3 到 6 个字符", trigger: "blur" }
        ],
        //验证密码
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          { min: 1, max: 6, message: "长度在 3 到 6 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    //重置表单的点击事件
    resLoginFrom() {
      //清除表单填写数据
      //清除表单的验证消息
      this.$refs.loginFromRef.resetFields();
    },
    //登陆的点击事件
    login() {
      this.$refs.loginFromRef.validate((valid, obj) => {
        console.log(valid);
        //验证没有成功
        if (!valid) {
          return;
        }
        //验证成功
        this.$http
          .post("/user/login", {
            username: this.loginFrom.username,
            password: this.loginFrom.password
          })
          .then(res => {
            console.log(res);
            console.log(res.data);
            //登录失败
            if (res.data.code !== 200) {
              return this.$message.error(res.data.msg);
            }
            //保存用户信息
            const userInfo = res.data.data;
            //保存用户的token
            if (res.data && res.data.token) {
              const token = res.data.token;
              setSessionStoreItem("token", token);
            }
            //保存用户信息到vuex中
            setSessionStoreItem("userInfo", JSON.stringify(userInfo));
            this.$store.commit("setUserInfo", JSON.stringify(userInfo));
            //登录成功
            //跳转页面
            this.$message.success(res.data.msg);
            this.$router.push("/main");
          },err=>{
            console.log(err);
          });
      });
    }
  }
};
</script>

<style lang="less" scoped>
.login-container {
  height: 100%;
  background-color: blue;
}
.login-box {
  width: 450px;
  height: 300px;
  background: #fff;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar-box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background: #ffffff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background: #eee;
    }
  }
}
.login-from {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
